///
/// ArmyBodger 3
/// (c)2010 richyp
///
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace ArmyBodger.Core {
    /// <summary>
    /// Represents the Header portion of a GameSystem or ArmyBook object
    /// </summary>
    public class Header : DynamicObject {
        /// <summary>
        /// Internal list of properties that can be accessed directly using
        /// the .Net 4.0 dynamic API
        /// </summary>
        public Dictionary<string, object> Properties { get; set; }
        /// <summary>
        /// Default Constructor
        /// </summary>
        public Header() {
            this.Properties = new Dictionary<string, object>();
        }
        /// <summary>
        /// Look to see if the specified Property exists in the Properties collection
        /// e.g. Header.MyProperty checks to see if this.Properties["MyProperty"] exists
        /// </summary>
        /// <param name="binder">Dynamic API binder</param>
        /// <param name="result">The resultant object</param>
        /// <returns>True on success otherwise false</returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            if (this.Properties.ContainsKey(binder.Name)) {
                result = this.Properties[binder.Name];
                return true;
            } else {
                result = false;
                return true;
            }
        }
        /// <summary>
        /// Set the value in this.Properties[binder.Name] to that specified
        /// e.g. this.Foo = "Bar" tries to do this.Properties["Foo"] = "Bar"
        /// </summary>
        /// <param name="binder">The Dynamic API binder</param>
        /// <param name="value">The value to set the property to</param>
        /// <returns>True on success otherwise false</returns>
        public override bool TrySetMember(SetMemberBinder binder, object value) {
            if (this.Properties == null) {
                this.Properties = new Dictionary<string, object>();
            }
            if (this.Properties.ContainsKey(binder.Name)) {
                this.Properties[binder.Name] = value;
            } else {
                this.Properties.Add(binder.Name, value);
            }
            return true;
        }
    }
}
